package com.jlnx.controller.bank;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jlnx.dao.ChainEnterpriseDaoImpl;
import com.jlnx.dao.CoreEnterpriseDaoImpl;
import com.jlnx.dao.EnterpriseDaoImpl;
import com.jlnx.dao.FinanceReqDaoImpl;
import com.jlnx.pojo.ChainEnterprise;
import com.jlnx.pojo.CoreEnterprise;
import com.jlnx.pojo.Enterprise;
import com.jlnx.pojo.FinanceReq;

/*
 * 修改限额和信用额度，包括核心企业和链条企业
 */
@WebServlet("/updateAmountController")
public class updateAmountController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求信息
		response.setContentType("charset=UTF-8");
		//设置请求编码模式
    	request.setCharacterEncoding("utf-8");
    	String entNo = request.getParameter("entNo");
    	int role = Integer.parseInt(request.getParameter("role"));
		String limitStr = request.getParameter("limit");
		String creditLimitStr = request.getParameter("creditLimit");
        
		
		System.out.println("更新限额：" + entNo);
		
		BigDecimal limit = null;
		BigDecimal creditLimit = null;
		
		if(limitStr != null && limitStr != "") {
			limit = new BigDecimal(limitStr);
		}
		
		if(creditLimitStr != null && creditLimitStr != "") {
			creditLimit = new BigDecimal(creditLimitStr);
		}
		
		ChainEnterpriseDaoImpl chaindao = new ChainEnterpriseDaoImpl();
		CoreEnterpriseDaoImpl coredao = new CoreEnterpriseDaoImpl();
		
		if(role == 1) {//更新核心企业限额信息
			CoreEnterprise coreEnterprise = coredao.selectByEntNo(entNo);
			//获取链条企业现在的信用额度信息
			BigDecimal creditLimitOld = coreEnterprise.getCreditLimit();
			//获取链条企业现在的剩余额度信息
			BigDecimal ramainLimitOld = coreEnterprise.getRemainLimit();
			//记录本次修改后限额变化(修改后减修改前)
			BigDecimal sum = creditLimit.subtract(creditLimitOld);
			//计算修改后的剩余限额，过去剩余加变化
			BigDecimal ramainLimit = ramainLimitOld.add(sum);
			
			coreEnterprise.setLimit(limit);
			coreEnterprise.setCreditLimit(creditLimit);
			coreEnterprise.setRemainLimit(ramainLimit);
			 //插入修改后的链条企业信息
			if(coredao.updateCoreEntMessage(coreEnterprise) < 0) {
				request.getRequestDispatcher("/InEnterpriseSearchController").forward(request, response);
				
			}else {
				request.getRequestDispatcher("/InEnterpriseSearchController").forward(request, response);
			}
		}else if(role == 3) {//更新链条企业限额信息
			ChainEnterprise chainEnterprise = chaindao.selectByEntNo(entNo);
			//获取链条企业现在的信用额度信息
			BigDecimal creditLimitOld = chainEnterprise.getCreditLimit();
			//获取链条企业现在的剩余额度信息
			BigDecimal ramainLimitOld = chainEnterprise.getRemainLimit();
			//记录本次修改后限额变化(修改后减修改前)
			BigDecimal sum = creditLimit.subtract(creditLimitOld);
			//计算修改后的剩余限额，过去剩余加变化
			BigDecimal ramainLimit = ramainLimitOld.add(sum);
			
			chainEnterprise.setLimit(limit);
			chainEnterprise.setCreditLimit(creditLimit);
			chainEnterprise.setRemainLimit(ramainLimit);
			
			
			
			
			
			 //插入修改后的链条企业信息
			if(chaindao.updateChainEntMessage(chainEnterprise) < 0) {
				request.getRequestDispatcher("/main/amountManage.jsp").forward(request, response);
				request.setAttribute("isRequestSuccess", false);
			}else {
				request.setAttribute("isRequestSuccess", true);
				request.getRequestDispatcher("/InEnterpriseSearchController").forward(request, response);

			}
		}
				
	}

}
